package com.hspedu.writer_;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author LiuJinTao
 * @className FileWriter_
 * @date 2024/06/27 20:15
 * @description TODO
 */
public class FileWriter_ {
    public static void main(String[] args) {

        // 创建一个 FileWriter 对象
        String path = "e:\\note.txt";
        FileWriter fileWriter = null;
        char[] chars = {'a', 'b', 'c'};
        try {
            fileWriter = new FileWriter(path);
            // write(int) 写入单个字符
//            fileWriter.write('H');

            // write(char[]) 写入指定数组
//            fileWriter.write(chars);

            // write(char[], off, len) 写入指定数组的指定部分
//            fileWriter.write("刘金涛帅哥".toCharArray(), 0, 3);

            // write(String) 写入整个字符串
//            fileWriter.write("你好哈哈哈哈");

            // write(String, off, len) 写入字符串的指定部分
//            fileWriter.write("你好哈哈哈", 0, 2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 一定要关闭或者 flush 才能把数据写入文件
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
